#include "kdcmainwindowframe.h"

#include <kdclib.h>

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QVariant>

#include <KIcon>
#include <KLocale>
#include <KPluginInfo>

KDCMainWindowFramePrivate::KDCMainWindowFramePrivate( KDCMainWindowFrame *aParent, KDC::Position position )
 : QObject( aParent ), p_position( position ), p_id( -1 ), p_tabBars( NULL ), p_buttons( NULL ), p_stack( NULL ), p_tabWidget( NULL )
{
	if ( position == KDC::Center )
	{
		p_tabWidget = new KTabWidget;
	}
	else
	{
		if ( position == KDC::Left )
			p_tabBars = new KMultiTabBar( KMultiTabBar::Left );
		else
			if ( position == KDC::Right )
				p_tabBars = new KMultiTabBar( KMultiTabBar::Right );
		else
			p_tabBars = new KMultiTabBar( KMultiTabBar::Bottom );
		p_tabBars->setStyle( KMultiTabBar::KDEV3ICON );
		p_tabBars->setMinimumWidth( 26 );

		p_stack = new QStackedWidget;
		p_stack->setVisible( FALSE );
		p_buttons = new QButtonGroup;
		p_buttons->setExclusive( TRUE );
		connect( p_buttons, SIGNAL( buttonClicked( int ) ), this, SLOT( slotButtonClicked( int ) ) );
	}
}

KDCMainWindowFramePrivate::~KDCMainWindowFramePrivate()
{
	//deletePtr( p_buttons );
	//deletePtr( p_tabBars );
	//deletePtr( widget() );
}

KDC::Position KDCMainWindowFramePrivate::position() const
{
	return p_position;
}

int KDCMainWindowFramePrivate::count() const
{
	return ( p_position == KDC::Center ) ? p_tabWidget->count() : p_stack->count();
}

void KDCMainWindowFramePrivate::addGuiWidget( QWidget *aWidget, const QString &aName, const KIcon &aIcon )
{
	if ( p_position == KDC::Center )
	{
		p_tabWidget->addTab( aWidget, aIcon, aName );
		p_tabWidget->setTabBarHidden( p_tabWidget->count() == 1 );
	}
	else
	{
		int index = p_stack->addWidget( aWidget );
		p_tabBars->appendTab( aIcon.pixmap( 26, 26 ), index, aName );
		p_buttons->addButton( p_tabBars->tab( index ), index );
	}
}

void KDCMainWindowFramePrivate::removeGuiWidget( QWidget *aWidget )
{
	if ( p_position == KDC::Center )
	{
		for ( int i=0; i<p_tabWidget->count(); ++ i )
			if ( p_tabWidget->widget( i ) == aWidget )
			{
				p_tabWidget->removeTab( i );
				return;
			}
	}
	else
	{
		int index = p_stack->indexOf( aWidget );
		p_buttons->removeButton( p_tabBars->tab( index ) );
		p_stack->removeWidget( aWidget );
		p_tabBars->removeTab( index );
	}
}

void KDCMainWindowFramePrivate::hide()
{
	if ( p_id != -1 )
		slotButtonClicked( p_id );
	( p_position == KDC::Center ) ? p_tabWidget->setEnabled( FALSE ) : p_tabBars->setEnabled( FALSE );
}

void KDCMainWindowFramePrivate::show()
{
	( p_position == KDC::Center ) ? p_tabWidget->setEnabled( TRUE ) : p_tabBars->setEnabled( TRUE );
}

void KDCMainWindowFramePrivate::slotButtonClicked( int id )
{
	if ( p_id == id && p_tabBars->tab( id )->isChecked() )
	{
		p_buttons->setExclusive( FALSE );
		p_tabBars->tab( id )->setChecked( FALSE );
		p_buttons->setExclusive( TRUE );
		p_id = -1;
		p_stack->setVisible( FALSE );
	}
	else
	{
		p_id = id;
		if ( !p_stack->isVisible() )
		{
			p_stack->setVisible( TRUE );
		}
		p_stack->setCurrentIndex( id );
	}
}

KMultiTabBar* KDCMainWindowFramePrivate::tabBars() const
{
	return p_tabBars;
}

QWidget* KDCMainWindowFramePrivate::widget() const
{
	return ( p_position == KDC::Center ) ? qobject_cast<QWidget*>( p_tabWidget ) : qobject_cast<QWidget*>( p_stack );
}

KDCMainWindowFrame::KDCMainWindowFrame( QWidget *parent ) : QWidget( parent )
{
	QSplitter *p_horizontalSplitter = new QSplitter( Qt::Horizontal, this );
	QSplitter *p_verticalSplitter = new QSplitter( Qt::Vertical, this );
	QSplitter *p_horizontalSplitterL = new QSplitter( Qt::Horizontal, this );

	KDCMainWindowFramePrivate *ptr;

	QWidget *topWidget = new QWidget( this );
	QHBoxLayout *topLayout = new QHBoxLayout( topWidget );
	//ptr = new KDCMainWindowFramePrivate( this, KDC::Left );
	//p_tabBars.append( ptr );
	//topLayout->addWidget( ptr->tabBars() );
	//p_horizontalSplitter->addWidget( ptr->widget() );
	topLayout->addWidget( p_horizontalSplitter );

	ptr = new KDCMainWindowFramePrivate( this, KDC::Center );
	p_tabBars.append( ptr );
	p_horizontalSplitter->addWidget( ptr->widget() );
	
	ptr = new KDCMainWindowFramePrivate( this, KDC::Right );
	p_tabBars.append( ptr );
	topLayout->addWidget( ptr->tabBars() );
	p_horizontalSplitter->addWidget( ptr->widget() );
	p_horizontalSplitter->setStretchFactor( 0, 20 );

	QWidget *bottomWidget = new QWidget( this );
	QVBoxLayout *bottomLayout = new QVBoxLayout( bottomWidget );
	bottomLayout->addWidget( p_verticalSplitter );
	ptr = new KDCMainWindowFramePrivate( this, KDC::Bottom );
	p_tabBars.append( ptr );
	bottomLayout->addWidget( ptr->tabBars() );
	p_verticalSplitter->addWidget( topWidget );
	p_verticalSplitter->addWidget( ptr->widget() );
	p_verticalSplitter->setStretchFactor( 0, 20 );

	QWidget *leftWidget = new QWidget( this );
	QHBoxLayout *leftLayout = new QHBoxLayout( leftWidget );	
	ptr = new KDCMainWindowFramePrivate( this, KDC::Left );
	p_tabBars.append( ptr );
	leftLayout->addWidget( ptr->tabBars() );
	p_horizontalSplitterL->addWidget( ptr->widget() );
	leftLayout->addWidget( p_horizontalSplitterL );
	p_horizontalSplitterL->addWidget( bottomWidget );
	p_horizontalSplitterL->setStretchFactor( 1, 20 );
	
	QVBoxLayout *mainLayout = new QVBoxLayout( this );
	mainLayout->addWidget( leftWidget );
}

KDCMainWindowFrame::~KDCMainWindowFrame()
{

}

void KDCMainWindowFrame::addPlugin( KDCPluginInterface *interface )
{
	if ( !interface->widget() )
		return;

	KDCMainWindowFramePrivate *ptr;
	foreach ( ptr, p_tabBars )
	{
		if ( ptr->position() == interface->tabPosition() )
		{
			ptr->addGuiWidget( interface->widget(), interface->info().name(), KIcon( interface->info().icon() ) );
			break;
		}
	}
}

void KDCMainWindowFrame::removePlugin( KDCPluginInterface *interface )
{
	if ( !interface->widget() )
		return;

	KDCMainWindowFramePrivate *ptr;
	foreach ( ptr, p_tabBars )
	{
		if ( ptr->position() == interface->tabPosition() )
		{
			ptr->removeGuiWidget( interface->widget() );
			break;
		}
	}
}

void KDCMainWindowFrame::setupGui()
{
	KDCMainWindowFramePrivate *ptr;
	foreach ( ptr, p_tabBars )
	{
		//if ( ptr->count() == 0 )
		//	ptr->tabBars()->hide();
	}
}
